<?php

class impoort_userTask extends sfBaseTask
{
  protected function configure()
  {
    // // add your own arguments here
    // $this->addArguments(array(
    //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
    // ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
      // add your own options here
    ));

    $this->namespace        = '';
    $this->name             = 'impoort_user';
    $this->briefDescription = '';
    $this->detailedDescription = <<<EOF
The [impoort_user|INFO] task does things.
Call it with:

  [php symfony impoort_user|INFO]
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    // add your code here
    $file_data = file_get_contents(sfConfig::get('sf_upload_dir').'/./user.txt');
    $explode_users = explode("\n", $file_data);
    $cover_dir = sfConfig::get('sf_upload_dir').'/./user_cover/';
    
    foreach($explode_users as $user){
        
        $user_info = explode('|', $user);
        $user = $user_info[0];
        $user_children = $user_info[1];
        $user_info = explode(',', $user);
        
        if($user_info[0] != '13700000095' && $user_info[0] != '13700000099'){ 
            continue;
        }
        
        $user_children_info = explode(',', $user_children);
        $phone = $user_info[0];
        $province = $user_info[1];
        $city = $user_info[2];
        $sex_array = array_flip(User::$sex); // 1男，0女
        $role_array = array_flip(User::$roles);
        $sex = $sex_array[trim($user_info[3])];
        $nickname = $user_info[4];
        $role_id = $role_array[trim($user_info[5])];
        $cover = $cover_dir.$phone.'.jpg';
        $import_user = new User();
        $import_user->setPhone($phone)->setPassword(md5('12345678'))->setCover('')->setNickname($nickname)->setRole($role_id)->setSex($sex)->setCity($city)->setProvince($province)->save();
        $import_user->getUserAttr();
        $user_id = $import_user->getId();
        
        $child_name = $user_children_info[0];
        $year = $user_children_info[1];
        $month = $user_children_info[2];
        $child_sex = $sex_array[trim($user_children_info[3])];

        $child = new UserChild();
        $child->setUserId($user_id)->setName($child_name)->setYear($year)->setMonth($month)->setSex($child_sex)->save();
          
        $year = date('Y',time());
        $month = date('m',time());
        $day = date('d',time());
        $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.jpg';
        $upyun_save_path = '/user/cover/'.$user_id.'/'.$save_file_name;
        $upyun = new UpYun('onetongxue','onetongxue' , 'onetongxue2015');

        try {
            $fh = fopen($cover, 'rb');
            $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
            fclose($fh);
            $image_url=  'http://img.onetongxue.com/'.$upyun_save_path;
            $import_user->setCover($image_url)->save();

        } catch(Exception $e) {
            echo $e->getCode();
            echo $e->getMessage();
        }
        
        echo $user_id."\n";
    }
  }
}
